"use strict";
$(document).ready(function() {

  // 更改密码处理  // 编辑处理
  $('#changpwddiv form').validate({
	    rules : {
	      newpwd: {
	    	required: true,
	    	minlength: 6
	      },
	      newpwd2 : {
	        equalTo : '#change_newpwd'
	      }
	    },
	    messages : {
	      newpwd : {	    	  
	        required : '密码不能为空',
	        minlength: '密码长度不能少于6位'
	      },
	      newpwd2 : {	    	  
	        equalTo : '两次输入的密码必须相同'
	      }
	    },
	    errorClass : 'has-error',
	    validClass : 'has-success',
	    highlight : function(element, errorClass, validClass) {
	      var $c = $(element).closest('div.form-group');
	      $c.addClass(errorClass).removeClass(validClass);
	    },
	    unhighlight : function(element, errorClass, validClass) {
	      var $c = $(element).closest('div.form-group');
	      $c.removeClass(errorClass).addClass(validClass);
	    }
	  });
  var changePwdDlg;
  $('#changepwd').click(function() {
	  changePwdDlg = layer.open({
      type : 1,
      title : false,
      area : '500px',
      closeBtn : true,
      shadeClose : false,
      content : $('#changpwddiv')
    });
    $('#changpwddiv form').get(0).reset();
  });
  $('#changpwddiv .btn-default').click(function() {
    layer.close(changePwdDlg);
  });
  $('#changpwddiv .btn-primary').click(function() {
    if (!$('#changpwddiv form').valid()) {
        layer.alert('请修正输入错误！');
        return;
    } 	  
    var data = {
      oldpwd : $('#change_oldpwd').val(),
      newpwd : $('#change_newpwd').val()
    };
    $.ajax({
      url : 'user/changepwd',
      method : 'POST',
      data : data,
      success : function(ret) {
        layer.close(changePwdDlg);
        layer.alert(ret ? '密码更改成功！' :  '密码更改失败！');
      }
    });
  });
});